/****************************************************************************
 *  _____       ______  _____
 * |_   _|     |  ____|/ ____|
 *   | |  _ __ | |__  | (___    Institute of Embedded Systems
 *   | | | '_ \|  __|  \___ \   Zürcher Hochschule für Angewandte
 *  _| |_| | | | |____ ____) |  Wissenschaften
 * |_____|_| |_|______|_____/   8401 Winterthur, Switzerland
 *
 ****************************************************************************
 *
 * @version $Rev: 146 $
 * @author  $Author: bacr/CRMA $
 * @date    $Date: 2013-10-02 17:56:25 +0200 (Mi, 02 Okt 2013) $
 *
 ***************************************************************************/

#ifndef __SPI_FRAME_H__
#define __SPI_FRAME_H__

#include "object.h"
#include "raw_spi_frame.h"
#include "hcpp/ptp2_slave.h"
#include "spi_ack_request.h"



#define SPI_FRAME(obj) ((spi_frame_t *) obj)

#define SLAVE_TABLE_HOST_ADDRESS    (0x02)
#define SPI_PROT_VERSION            (1)
#define SPI_MESSAGE_MAX_LEN         (128)


typedef enum{
    eSpiAckRequest         = 0x01,
    eSpiSlaveTableRequest,
    eSpiSlaveRecordSet,
    eSpiSystemMonitoringSet,
    eMaxSpiFrameType
}spi_frame_type_t;



typedef struct{
    uint8_t             dstAddress;
    spi_frame_type_t    eType;
    uint8_t             payloadLen;
    uint8_t             protocolVer;
    uint16_t            seqNumber;
    uint16_t            checkSum;
}spi_frame_header_t;



typedef struct _spi_payload_t{
    union{
      ptp2_slave_t          slave_record;
      spi_ack_request_t     ack_request;
    };
}spi_payload_t;


typedef struct _spi_frame_t {
    //object_t                obj;
    spi_frame_header_t      header;
    spi_payload_t           payload;
}spi_frame_t;


// pointer to function declaration for SPI frame encoding
typedef bool (*spi_master_frame_builder)(void*, uint8_t, spi_frame_type_t, spi_frame_t*);


extern bool    spi_frame_header_init(spi_frame_t *this);
extern bool    spi_frame_encode(void *raw_data, uint8_t length, spi_frame_type_t, spi_frame_t*);
extern void    spi_frame_decode(spi_frame_t *this, raw_spi_frame_t *raw_spi_frame);

extern bool    slave_record_encoder(void *raw_data, uint8_t length, spi_frame_type_t frame_type,
                                    spi_frame_t *frame);

#endif  // __SPI_FRAME_H__




